package com.yanqu.road.server.cmd.activity.seacraftsouthsea;

import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.activity.SeacraftSouthSeaProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.seacraft.SeacraftSouthSeaModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.gameplayer.module.vehicle.VehicleModule;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftSouthSeaMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;


/**
 * @Description
 * @Author cwq
 * @Data 2022/1/20 15:28
 */
@Cmd(code = GameProtocol.S_SEACRAFT_SOUTH_SEA_ACTION_FROM_CROSS, desc = "海上争霸 南海丝路 进攻 / 突袭 / 使用道具进攻 / 修复 跨服返回区服 协议")
public class SeacraftSouthSeaActionFromCrossCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SeacraftSouthSeaProto.SeacraftSouthSeaActionCrossToGameMsg crossToGameMsg = SeacraftSouthSeaProto.SeacraftSouthSeaActionCrossToGameMsg.parseFrom(packet.getBytes());
        SeacraftSouthSeaProto.SeacraftSouthSeaActionRespMsg.Builder stcMsg = crossToGameMsg.getStcMsg().toBuilder();
        // 取消预扣除道具
        if(crossToGameMsg.getType() == 0 || crossToGameMsg.getType() == 2 || crossToGameMsg.getType() == 3 || crossToGameMsg.getType() == 5){
            // 取消 预扣除
            Property property = new Property(crossToGameMsg.getPropId(), crossToGameMsg.getCount());
            player.getModule(PreReduceModule.class).restorePreReduce(property);
        }
        if(stcMsg.getRet() == 0){
            if(crossToGameMsg.getType() == 0 || crossToGameMsg.getType() == 1){
                //珍兽绑定 鱼类绑定
                for (int patronsId : crossToGameMsg.getPatronsIdList()){
                    player.getModule(VehicleModule.class).bindPatrons(patronsId, SeacraftSouthSeaMgr.getActivityInfo().getActivityId());
                }
                if(crossToGameMsg.getType() == 0){
                    Property consume = new Property(crossToGameMsg.getPropId(), stcMsg.getRealConsumeNum());
                    player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.SeacraftActivity, eLogMoneyType.SeacraftActivityAddAttackCost);
                }
            }else if(crossToGameMsg.getType() == 2){
                // 道具攻击
                Property consume = new Property(crossToGameMsg.getPropId(), crossToGameMsg.getCount());
                player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.SeacraftActivity, eLogMoneyType.SeacraftActivityPropAttackCost);
                // 加宝箱奖励
                OpenGoodsBagResult openGoodsBagResult = GoodsBagMgr.getRandomGoods(crossToGameMsg.getPropId());
                player.getModule(CurrencyModule.class).addCurrency(openGoodsBagResult.getReward(), eLogMoneyType.SeacraftActivity, eLogMoneyType.SeacraftActivityPropAttackReward);
                stcMsg.setReward(PropertyHelper.parsePropertyToString(openGoodsBagResult.getReward()));
            }else if(crossToGameMsg.getType() == 3){
                // 道具修复
                Property consume = new Property(crossToGameMsg.getPropId(), stcMsg.getRealConsumeNum());
                player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.SeacraftActivity, eLogMoneyType.SeacraftActivityPropFixCost);
                // 加宝箱奖励
                Property reward = new Property();
                for (int i = 0; i < stcMsg.getRealConsumeNum(); i++) {
                    OpenGoodsBagResult openGoodsBagResult = GoodsBagMgr.getRandomGoods(crossToGameMsg.getPropId());
                    reward.addProperty(openGoodsBagResult.getReward());
                }
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.SeacraftActivity, eLogMoneyType.SeacraftActivityPropFixReward);
                stcMsg.setReward(PropertyHelper.parsePropertyToString(reward));
            }else if(crossToGameMsg.getType() == 5){
                // 使用船桨
                Property consume = new Property(crossToGameMsg.getPropId(), stcMsg.getRealConsumeNum());
                player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.SeacraftActivity, eLogMoneyType.SeacraftActivityPropSpeedCost);
                // 加宝箱奖励
                Property reward = new Property();
                for (int i = 0; i < stcMsg.getRealConsumeNum(); i++) {
                    OpenGoodsBagResult openGoodsBagResult = GoodsBagMgr.getRandomGoods(crossToGameMsg.getPropId());
                    reward.addProperty(openGoodsBagResult.getReward());
                }
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.SeacraftActivity, eLogMoneyType.SeacraftActivityPropSpeedReward);
                stcMsg.setReward(PropertyHelper.parsePropertyToString(reward));

            }
            player.getModule(SeacraftSouthSeaModule.class).synUserScore(stcMsg.getAllScore());
        }
        player.sendPacket(ClientProtocol.U_SEACRAFT_SOUTH_SEA_ACTION,stcMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
